package dao.impl;


import annotation.Check;
import dao.AddpowerDao;
import pojo.Addpower;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AddpowerDaoImpl implements AddpowerDao {
    private Connection conn = null;
    private ResultSet rs = null;
    private boolean flag = false;

    public ArrayList<Addpower> findAreaUserPower(Integer mid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Addpower> powers = new ArrayList<>();
        rs = JdbcUtils.selectsql("select * from addpower where mid='" + mid + "'");
//        while (true) {
//                if (!rs.next()) break;
//                Addpower addpower = new Addpower();
//                addpower.setAid(rs.getInt("aid"));
//                addpower.setMid(rs.getInt("mid"));
//                addpower.setUid(rs.getInt("uid"));
//                powers.add(addpower);
//        }
        int num = 0;
        do {
            num++;
            Addpower addpower = new Addpower();
            addpower = (Addpower) DataUtils.dataEncapsulation(Class.forName("pojo.Addpower"), rs);
            powers.add(addpower);
        } while (num < rs.getRow());
        JdbcUtils.closeconn(rs, null, conn);
        return powers;
    }


    @Check
    public boolean addAddpower(Integer mid, Integer uid) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "insert into addpower values(0,'" + uid + "','" + mid + "')";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        else flag = false;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    @Check
    public boolean deleteAddpower(Integer mid, Integer uid) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "delete from addpower where mid='" + mid + "' and uid='" + uid + "'";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public ArrayList<Addpower> findUserPower(Integer uid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Addpower> powers = new ArrayList<>();
        rs = JdbcUtils.selectsql("select * from addpower where uid='" + uid + "'");
//        while (true) {
//                if (!rs.next()) break;
//                Addpower addpower = new Addpower();
//                addpower.setAid(rs.getInt("aid"));
//                addpower.setMid(rs.getInt("mid"));
//                addpower.setUid(rs.getInt("uid"));
//                powers.add(addpower);
//        }
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Addpower addpower = new Addpower();
            addpower = (Addpower) DataUtils.dataEncapsulation(Class.forName("pojo.Addpower"), rs);
            powers.add(addpower);
        } while (row > 0);
        JdbcUtils.closeconn(rs, null, conn);
        return powers;
    }
}
